package interfaces.options;

import org.fenggui.Container;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.key.IKeyListener;
import org.fenggui.event.key.Key;
import org.fenggui.event.key.KeyPressedEvent;
import org.fenggui.event.key.KeyReleasedEvent;
import org.fenggui.event.key.KeyTypedEvent;
import org.fenggui.layout.BorderLayout;
import org.fenggui.layout.BorderLayoutData;
import org.fenggui.layout.RowLayout;
import org.fenggui.util.Color;


import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.OptionsWindowText;

import interfaces.GUISource;
import interfaces.menu.AbsMenu;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.InterfaceWindow;

public class OptionKeyWindow extends InterfaceWindow {
	
	private InterfaceButton okButton, cancelButton, changeButton;
	private String oldKey;
	private Lang doubleKeyDescription;
	private KeyOptionWidget sourceWidget;
	private InterfaceLabel keyLabel, doubleKeyLabel;
	private boolean keyPressed = true;
	
	public OptionKeyWindow(KeyOptionWidget sourceWidget, AbsMenu menu) {
		super(true, LanguageLoader.get(OptionsWindowText.Change_Key_for) + " " 
				+ LanguageLoader.get(sourceWidget.getDescription()));
		this.sourceWidget = sourceWidget;
		oldKey = sourceWidget.getValue();
		setXY(menu.getWidth() / 5 * 2, menu.getHeight() / 5 * 2);
		
		content.setLayoutManager(new BorderLayout());
		content.getAppearance().add(new PlainBackground(Color.LIGHT_BLUE));
		
		createNorthContent();
		createCenterContent();
		createSouthContent();
		
		pack();
	}
	
	private void createNorthContent() {
		Container northContent = new Container(new RowLayout(true));
		northContent.setLayoutData(BorderLayoutData.NORTH);
		
		String currentString = LanguageLoader.get(OptionsWindowText.Current_Keybind) + ":";
		InterfaceLabel currentLabel = new InterfaceLabel(currentString, GUISource.bigFont);
		northContent.addWidget(currentLabel);
		
		keyLabel = new InterfaceLabel(oldKey, GUISource.bigFont);
		northContent.addWidget(keyLabel);
		
		content.addWidget(northContent);
	}
	
	private void createCenterContent() {
		Container centerContent = new Container(new RowLayout(true));
		centerContent.setLayoutData(BorderLayoutData.CENTER);
		
		doubleKeyLabel = new InterfaceLabel(" ", GUISource.bigFont);
		centerContent.addWidget(doubleKeyLabel);
		
		content.addWidget(centerContent);
	}
	
	private void createSouthContent() {
		Container southContent = new Container(new RowLayout(true));
		southContent.setLayoutData(BorderLayoutData.SOUTH);
		
		ImageFont buttonFont = GUISource.bigFont;
		
		okButton = new InterfaceButton(LanguageLoader.get(OptionsWindowText.Ok), buttonFont);
		okButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				sourceWidget.getOptionsWindow().changeKeyBinding(sourceWidget, keyLabel.getText());
				if(doubleKeyDescription != null) 
					sourceWidget.getOptionsWindow().addDoubleKey(doubleKeyDescription);
				close();
			}
		});
		southContent.addWidget(okButton);
		
		changeButton = new InterfaceButton(LanguageLoader.get(OptionsWindowText.Change_Key), buttonFont);
		changeButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				keyPressed = false;
			}
		});
		changeButton.addKeyListener(getKeyListener());
		southContent.addWidget(changeButton);
		
		cancelButton = new InterfaceButton(LanguageLoader.get(OptionsWindowText.Cancel), buttonFont);
		cancelButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				close();
			}
		});
		southContent.addWidget(cancelButton);
		
		content.addWidget(southContent);
	}
	
	private IKeyListener getKeyListener() {
		return new IKeyListener() {
			@Override
			public void keyPressed(KeyPressedEvent keyPressedEvent) {
				if(keyPressed) return;
				String keyString = checkSpecialKey(keyPressedEvent.getKeyClass());
				if(keyString == null && String.valueOf(keyPressedEvent.getKey()).length() == 1) 
					keyString = String.valueOf(keyPressedEvent.getKey());
				else if(keyString == null && String.valueOf(keyPressedEvent.getKey()).equals("")) 
					keyString = "";
				if(String.valueOf(keyPressedEvent.getKey()).equals(" ")) keyString = "SPACE";
				if(String.valueOf(keyPressedEvent.getKey()).equals("	")) keyString = "TAB";
				if(keyString != null) keyString = keyString.toUpperCase();
				manageNewBinding(keyString);
			}
			
			@Override
			public void keyReleased(KeyReleasedEvent keyReleasedEvent) {}
			
			@Override
			public void keyTyped(KeyTypedEvent keyTypedEvent) {}
		};
	}
	
	private void manageNewBinding(String keyString) {
		keyLabel.setText(keyString);
		keyPressed = true;
		doubleKeyDescription = sourceWidget.getOptionsWindow().getDoubleKeyDescription(keyString);
		if(doubleKeyDescription != null && !doubleKeyDescription.equals(sourceWidget.getDescription())) {
			GUISource.setTextColor(keyLabel, Color.RED);
			doubleKeyLabel.setText(LanguageLoader.get(OptionsWindowText.Double_Key_found) + ": " 
								+ LanguageLoader.get(doubleKeyDescription));
		} else {
			doubleKeyDescription = null;
			doubleKeyLabel.setText(" ");
			GUISource.setTextColor(keyLabel, Color.BLACK);
		}
		pack();
	}
	
	private String checkSpecialKey(Key key) {
		if(key.equals(Key.ALT)) return "ALT";
		if(key.equals(Key.BACKSPACE)) return "<--";
		if(key.equals(Key.CTRL)) return "CTRL";
		if(key.equals(Key.DELETE)) return "DEL";
		if(key.equals(Key.DOWN)) return "Down";
		if(key.equals(Key.END)) return "END";
		if(key.equals(Key.ENTER)) return "ENTER";
		if(key.equals(Key.ESCAPE)) return "ESC";
		if(key.equals(Key.F1)) return "F1";
		if(key.equals(Key.F2)) return "F2";
		if(key.equals(Key.F3)) return "F3";
		if(key.equals(Key.F4)) return "F4";
		if(key.equals(Key.F5)) return "F5";
		if(key.equals(Key.F6)) return "F6";
		if(key.equals(Key.F7)) return "F7";
		if(key.equals(Key.F8)) return "F8";
		if(key.equals(Key.F9)) return "F9";
		if(key.equals(Key.F10)) return "F10";
		if(key.equals(Key.F11)) return "F11";
		if(key.equals(Key.F12)) return "F12";
		if(key.equals(Key.F6)) return "F6";
		if(key.equals(Key.HOME)) return "HOME";
		if(key.equals(Key.INSERT)) return "INSERT";
		if(key.equals(Key.LEFT)) return "LEFT";
		if(key.equals(Key.PAGE_DOWN)) return "PAGE DOWN";
		if(key.equals(Key.PAGE_UP)) return "PAGE UP";
		if(key.equals(Key.RIGHT)) return "RIGHT";
		if(key.equals(Key.SHIFT)) return "SHIFT";
		if(key.equals(Key.TAB)) return "TAB";
		if(key.equals(Key.UP)) return "UP";
		return null;
	}
}